package com.xiaoming.project.core.thread;

public class ThreadJoinTest {

    /**
     * 通过new Thread,每次start后马上调用join的方式控制线程执行顺序
     * 线程1，2之间有明确的执行顺序，可以控制多个线程之间的执行顺序
     */
    private static void threadJoinTest() {
        Thread thread1 = new Thread(new PrintTask("线程1"));
        Thread thread2 = new Thread(new PrintTask("线程2"));

        thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();
    }

    static class PrintTask implements Runnable {

        private String threadName;

        public PrintTask(String threadName) {
            this.threadName = threadName;
        }

        private void runTask() {
            System.out.printf("开始执行%s", threadName);
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("%s执行结束", threadName);
            System.out.println();
        }

        @Override
        public void run() {
            runTask();
        }

    }

}